{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ipywidgets as widgets\t# 控件库\\\n",
    "from utils.story_util import Story\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ipywidgets as widgets\t# 控件库\n",
    "from IPython.display import display\t# 显示控件的方法\n",
    "from ipywidgets import HBox, Label, VBox"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "output_dir = \"labeled_data\"\n",
    "title = widgets.Text(description=\"故事标题\",layout=widgets.Layout(width='50%', height='20px'))\n",
    "background = widgets.Textarea(description=\"故事背景\",layout=widgets.Layout(width='50%', height='100px'))\n",
    "action = widgets.Text(description=\"动作\")\n",
    "action_type = widgets.ToggleButtons(\n",
    "    options=[ '做','说', '描述',\"无动作\"],\n",
    "    description='动作种类:',\n",
    "    disabled=False,\n",
    "    button_style='', # 'success', 'info', 'warning', 'danger' or ''\n",
    "    layout=widgets.Layout(width='50%', height='50px')\n",
    "#     icons=['check'] * 3\n",
    ")\n",
    "result = widgets.Textarea(description=\"结果\",layout=widgets.Layout(width='50%', height='100px'))\n",
    "summary = widgets.Text(description=\"梗概\",layout=widgets.Layout(width='50%', height='20px'))\n",
    "\n",
    "submit = widgets.Button(description='提交',\n",
    "           layout=widgets.Layout(width='20%', height='40px'),\n",
    "          button_style='success')\n",
    "end_story = widgets.Button(description='结束故事',\n",
    "           layout=widgets.Layout(width='20%', height='40px'),\n",
    "          button_style='danger')\n",
    "\n",
    "button_box = HBox([submit,end_story])\n",
    "\n",
    "story = None\n",
    "def onsubmit(sender):\n",
    "    global story\n",
    "    if story is None:\n",
    "        story = Story(background.value,title.value)\n",
    "    story.add_action(action.value,action_type.value,result.value,summary.value)\n",
    "    action.value = \"\"\n",
    "    result.value = \"\"\n",
    "    summary.value = \"\"\n",
    "    \n",
    "def on_end_story(sender):\n",
    "    global story\n",
    "    action.value = \"\"\n",
    "    result.value = \"\"\n",
    "    summary.value = \"\"\n",
    "    title.value = \"\"\n",
    "    background.value = \"\"\n",
    "    with open(f\"{output_dir}/{story.title}.txt\",'w',encoding='utf-8') as whdl:\n",
    "        whdl.write(story.to_str())\n",
    "    story = None\n",
    "\n",
    "submit.on_click(onsubmit)\t# 回车以提交内容\n",
    "end_story.on_click(on_end_story)\n",
    "def display_all_stuff():\n",
    "    display(title,background,action_type,action,result,summary)\n",
    "    display(button_box)\n",
    "    \n",
    "    \n",
    "# 预览用的控件\n",
    "preview = widgets.Button(description='预览标注文件',\n",
    "           layout=widgets.Layout(width='20%', height='40px'),\n",
    "          button_style='warning')\n",
    "clear_preview = widgets.Button(description=' 清空预览',\n",
    "           layout=widgets.Layout(width='20%', height='40px'),\n",
    "          button_style='danger')\n",
    "preview_text = widgets.Textarea(layout=widgets.Layout(width='50%', height='100px'))\n",
    "\n",
    "preview_box = HBox([preview,clear_preview])\n",
    "def on_show_preview(w):\n",
    "    if story is not None:\n",
    "        preview_text.value = story.to_str()\n",
    "def on_clear_preview(w):\n",
    "    preview_text.value = \"\"\n",
    "preview.on_click(on_show_preview)\n",
    "clear_preview.on_click(on_clear_preview)\n",
    "    \n",
    "def display_preview_stuff():\n",
    "    display(preview_box)\n",
    "    display(preview_text)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 开始标注吧"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "31ff64cd6399434fb9556c6da3496b13",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Text(value='', description='故事标题', layout=Layout(height='20px', width='50%'))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "fa36ac7ba3004b1ba21abd17c913c74f",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Textarea(value='', description='故事背景', layout=Layout(height='100px', width='50%'))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "21f4d4fb8ff640928782f7db8d5f4681",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "ToggleButtons(description='动作种类:', layout=Layout(height='50px', width='50%'), options=('做', '说', '描述', '无动作'),…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "3ef65b1aeabd4d6f8aacc12de80eb45c",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Text(value='', description='动作')"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "caa124a124d942b7bf569b2d6f0cfba3",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Textarea(value='', description='结果', layout=Layout(height='100px', width='50%'))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "a939709027b84e73941a97075683e465",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Text(value='', description='梗概', layout=Layout(height='20px', width='50%'))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "2328f478ad5748f6914a1531e36d84c2",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(Button(button_style='success', description='提交', layout=Layout(height='40px', width='20%'), sty…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display_all_stuff()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "6f29bff77d764523b8dc1567e19a5778",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(Button(button_style='warning', description='预览标注文件', layout=Layout(height='40px', width='20%'),…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "683549a4ca2047a08ea81bf2cd1e4f1b",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Textarea(value='', layout=Layout(height='100px', width='50%'))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display_preview_stuff()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'NoneType' object has no attribute 'story'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-6-7aeb28780e3b>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mstory\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mstory\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m: 'NoneType' object has no attribute 'story'"
     ]
    }
   ],
   "source": [
    "len(story.story)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
